import {Component, OnInit, ViewChild} from '@angular/core'
import { ApiService } from '../../services/api.service'
import { EchartService } from '@app/shared/utils/echart.service'
import {CursorService} from "@core/http/cursor.service";
import {DataTableComponent} from "@shared/components/data-table/data-table.component";

@Component({
  selector: 'app-customer-tag',
  templateUrl: './customer-tag.component.html',
  styleUrls: ['./customer-tag.component.scss'],
  providers: [ApiService]
})
export class CustomerTagComponent implements OnInit {
  @ViewChild(DataTableComponent,{static:true})
  dataTable:DataTableComponent
  public catalogList: any[] = [];
  public tagList: any[] = [];
  public hotList: any[] = [];
  public activeList: any[] = [];
  public newList: any[] = [];
  public tagCloudList: any[] = [];
  public tagOptions: Array<any> =[];
  catalogcode ='';
  echartsIntance: any = null;
  // public wordCloudImg = require('../../../assets/images/image1.png')
  constructor(private echartService: EchartService,
              private apiService: ApiService,
              public cursorService: CursorService,
              ) {}
  // echarts初始化事件，拿到instance为后续图形数据变化准备

  ngOnInit() {
    this.getTagStatistics();
    this.getTagHot();
    this.getTagActive();
    this.getTagCatalog();
    // this.getTagCatalogList()
    // this.apiService.getCustomerTagList('tag').subscribe(list => {
    //   // 生成树形结构
    //   this.hotList = list
    //     .slice(250, 280)
    //     .sort(() => 0.5 - Math.random())
    //     .slice(0, 5)
    //
    //   this.newList = list
    //     .slice(80, 100)
    //     .sort(() => 0.5 - Math.random())
    //     .slice(0, 5)
    //
    //   this.tagCloudList = list
    //     .slice(0, 20)
    //     .sort(() => 0.5 - Math.random())
    //     .map(x => x.name)
    //
    //   this.tagOptions = this.echartService.getOption({
    //     type: 'pie',
    //     data: [
    //       {
    //         name: '基础特征',
    //         value: 98
    //       },
    //       {
    //         name: '产品特征',
    //         value: 121
    //       },
    //       {
    //         name: '金融特征',
    //         value: 78
    //       },
    //       {
    //         name: '价值特征',
    //         value: 64
    //       },
    //       {
    //         name: '风险特征',
    //         value: 39
    //       },
    //       {
    //         name: '行为特征',
    //         value: 89
    //       },
    //       {
    //         name: '营销特征',
    //         value: 101
    //       }
    //     ],
    //     update: config => {
    //       config.legend = undefined
    //       return config
    //     }
    //   })
    // })
  }
  //获取标签统计列表
  public getTagStatistics(){

    this.apiService.getTagStatistics().subscribe(list => {
      this.tagOptions = this.echartService.getOption({
          type: 'pie',
          data: [
            {
                name:'金融特征',
                value: 1
              },
              {
                name:'金融特征',
                value: 1
              },
              {
                name:'金融特征',
                value: 3
              },
            {
                name: '风险特征',
                value: 3
            },
            {
              name: '行为特征',
              value: 4
            },
            {
              name: '营销特征',
              value: 5
            }
          ],
          update: config => {
            config.legend = undefined
            return config
          }
        })

    })
  }
  //获取热门标签
  public getTagHot(){

    this.apiService.getTagHot().subscribe(list => {
      this.hotList = list
    })
  }
  //获取活跃标签
  public getTagActive(){
    this.apiService.getTagActive().subscribe(list => {
      this.activeList = list
    })
  }
  //获取标签目录
  public getTagCatalog(){
    this.cursorService.limit = 10000;

    this.apiService.getTagCatalog(this.cursorService).subscribe(list => {
     for(let item of list){
       if(item['level']){
         this.catalogList.push(item)
       }
     }
      for(let item of this.catalogList){
        let data =[];
        for(let item2 of list) {
          if (item2['pcode']&&item2['pcode']===item['catalogcode']) {
            data.push(item2)
          }
        }
        item['children'] = data
      }
      this.catalogList = JSON.parse(JSON.stringify(this.catalogList).replace(/catalogname/g, 'label'));
      this.catalogList = JSON.parse(JSON.stringify(this.catalogList).replace(/catalogcode/g, 'id'));
    })
  }
  public onCatalogChange($event) {
    if ($event['action'] === 'click') {
      this.getCustTagList($event['treeNodeID'])
    }
  }
  //获取标签列表
  public getCustTagList(id){
    if(this.catalogcode!=id){
       this.dataTable.reset()
    }
    this.catalogcode = id;
    this.tagList = [];
    this.cursorService.limit =1;
    this.cursorService.query().eq('catalogcode',id)
    // this.cursorService.statement ="select * where catalogcode="+"'"+id+"'";
    this.apiService.getCustTagList(this.cursorService).subscribe(list => {
      this.tagList = list
    })
    this.cursorService.query({reset:true})
  }
  // public onSelectTag(...a) {}

  // private getTagCatalogList() {
  //   this.apiService.getCustomerTagList('catalog').subscribe(list => {
  //     // 添加label字段
  //     list.forEach((x: any) => {
  //       x.label = x.name
  //     })
  //     // 生成树形结构
  //     this.catalogList = this.generateCatalogTree(list)
  //   })
  // }

  // private generateCatalogTree(list) {
  //   const root = list.filter(x => !x.parent)
  //   const depth = node => {
  //     const children = list.filter(x => x.parent === node.id)
  //     if (children && children.length) {
  //       node.children = children
  //       node.children.forEach(depth)
  //     } else {
  //       node.leaf = true
  //     }
  //     return node
  //   }
  //   return root.map(depth)
  // }

  /**
   * 选择标签类型改变
   * @param param0
   */
  // public onCatalogChange({ action, treeNodeID }) {
  //   if (action === 'click') {
  //     this.getTagList(treeNodeID)
  //   }
  // }

  /**
   * 获取标签列表
   * @param parent
   */
  // private getTagList(parent) {
  //   this.apiService.getCustomerTagList('tag', parent).subscribe(list => {
  //     // 生成树形结构
  //     this.tagList = list
  //     console.log('222',JSON.stringify(list,null,4))
  //   })
  // }
}
